<?php
/**
 * @title 95. 不同的二叉搜索树 II - 递归缓存
 * @author start2004
 */


class TreeNode {
    public $val = null;
    public $left = null;
    public $right = null;
    function __construct($val = 0, $left = null, $right = null) {
        $this->val = $val;
        $this->left = $left;
        $this->right = $right;
    }
}


class Solution {

    /**
     * @param Integer $n
     * @return TreeNode[]
     */
    function generateTrees($n) {
        /**
         * @since 2020-07-21 特殊情况先行排除
         */
        if($n == 0){
            return [];
        } else {}

        /**
         * @since 2020-07-21 节点
         */
        $nodes = [];
        for($i=1; $i<=$n; $i++){
            $nodes[] = $i;
        }

        /**
         * @since 2020-07-21 避免反复计算
         */
        $this->trees = [];

        /**
         * @since 2020-07-21 获取子树
         */
        $trees = self::tree($nodes);
        return $trees;

        /**
         * @since 2020-07-21 转成数组
         */
        $data = self::toArray($trees);

        /**
         * @return
         */
        return $data;
    }


    /**
     * @title 返回二叉树
     * @author start2004
     * @since 2020-07-21 Created
     *
     * @param array $nodes
     * @return array
     */
    function tree($nodes = []){
        $len = count($nodes);
        // echo str_repeat(PHP_EOL, 2), "nodes=[", implode(", ", $nodes) ,"]", PHP_EOL;

        // $key = md5(json_encode($nodes));
        $key = implode("-", $nodes);
        if(isset($this->trees[$key])){
            return $this->trees[$key];
        } else {}

        /**
         * @since 2020-07-21 单独处理长度0和1
         */
        if($len == 0){
            $result = [null];
        } elseif($len == 1){
            $result = [new TreeNode($nodes[0])];
        } else {
            /**
             * @since 2020-07-21 root节点从左往右移动, shift, push
             * @since 2020-07-21 避免使用array_slice
             */
            $midNode = array_shift($nodes);
            $leftNodes = [];
            $rightNodes = $nodes;

            /**
             * @since 2020-07-21 递归
             */
            $result = [];
            for($i=1; $i<=$len; $i++){
                // echo "mid={$midNode}, left=[", implode(", ", $leftNodes) ,"], right=[", implode(", ", $rightNodes) ,"]", PHP_EOL;

                /**
                 * @since 2020-07-21 左右子树
                 */
                $leftTrees = self::tree($leftNodes);
                $rightTrees = self::tree($rightNodes);

                /**
                 * @since 2020-07-21 根节点
                 */
                // $root = new TreeNode($midNode);
                foreach ($leftTrees as $left){
                    foreach ($rightTrees as $right){
                        $root = new TreeNode($midNode);
                        // $root = $this->roots[$midNode];
                        $root->left = $left;
                        $root->right = $right;
                        $result[] = $root;
                        // unset($root);
                    }
                }

                /**
                 * @since 2020-07-21 root向右移动
                 */
                array_push($leftNodes, $midNode);
                $midNode = array_shift($rightNodes);
            }
        }

        /**
         * @since 2020-07-21 hash缓存
         */
        $this->trees[$key] = $result;

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.21_2.php
 */
$datas = [
    3,
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->generateTrees($data);
    print_r($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
